/*
	---------------------------------------------
	-	Main Javascript functions
	-	/core.site.js
	-	Namespace, major scripts etc...
	---------------------------------------------
	-	Author:	James Haley
	-			james.haley@wapisasa.com
	-			24/09/2013 13:25:37
	---------------------------------------------
*/

	var site={};

	//	++ Initiation
	//----------------------------------------------
	//	First run scripts, adding listeners etc...
	
		(site.setup = function() {
			return {
				_initial:[],
				run:function(){
					// Show core loaded
					DEBUG('Core Initialised');

					// Run through requested functions
					site.setup._initial.each(
						function(o){
							o.call();
						}
					);
				}
			}
		}());


	//	++ Extras
	//----------------------------------------------
	//	Scripts that need to run ASAP but don't fit anywhere else
	
		// ++ Ask jQuery to play nice
		jQuery.noConflict();


	//	++ DEBUG
	//----------------------------------------------
	//	Prepare a DEBUG function for if we are in development
	
		var DEBUG = function(){};

		// IE Console bug fix 
		if (!window.console) {
			var names = ["log", "debug", "info", "warn", "error", "assert", "dir", "dirxml",
			"group", "groupEnd", "time", "timeEnd", "count", "trace", "profile", "profileEnd"];
			window.console = {};
			for (var i = 0; i < names.length; ++i) {
				window.console[names[i]] = function() {};
			}
		};

	//	++ Prototypes
	//----------------------------------------------
	//	Helpful little scripts that make things easy
	
		//	++ Array Remove - By John Resig (MIT Licensed)
		//----------------------------------------------
		Array.prototype.remove = function(from, to) {
			var rest = this.slice((to || from) + 1 || this.length);
			this.length = from < 0 ? this.length + from : from;
			return this.push.apply(this, rest);
		};
		
		//	++ Trim spaces
		//----------------------------------------------
		String.prototype.trim = String.prototype.trim || function() {
			return this.replace(/^\s+|\s+$/,"");
		}
		
		//	++ Remove last full stop prototype
		//----------------------------------------------
		String.prototype.trimFullStops = function() {
			return this.replace(/^\.+|\.+$/,"");
		};
		
		//	++ New line remove prototype
		//----------------------------------------------
		String.prototype.replaceNewLine = function() {
			return this.replace(/(\r\n|\r|\n)/g, "<br />");
		}
		
		//	++ Replace breaks remove prototype
		//----------------------------------------------
		String.prototype.replaceBreaks = function() {
			return this.replace(/<br \/>|<br\/>/g, "\n");
		}
		
		//	++ String Trim to length or first Stop(.)
		//----------------------------------------------
		String.prototype.short = function(nLen) {
			var nFSPos=this.indexOf('.');
			return (this.length>nLen)?((nFSPos>-1)&&(nFSPos<nLen+1)&&(nFSPos>3))?this.split('.')[0].trim()+'…':this.substring(0,nLen).trim()+'…':this;
		};
		
		//	++ Encode for URL transport
		//----------------------------------------------
		String.prototype.encode = function() {
			return (this.length>0)?encodeURIComponent(this):this;
		};
		
		//	++ Replace JS quotes
		//----------------------------------------------
		String.prototype.replaceQuotes = function() {
			return this.replace(/"/g,"\\\"");
		}
			
		//	++ HTML remove tags prototype
		//----------------------------------------------
			String.prototype.stripTags = function() {
				return this.replace(/<\S[^>]*>/g, "");
			}
		
		//	++ Fix Numeric
		//----------------------------------------------
		String.prototype.tidyNumeric = function() {
			return Math.abs(this.replace(/[^0-9.]/ig,'').trimFullStops());
		};
		
		//	++ Tidy Decimal
		//----------------------------------------------
		Number.prototype.tidyDecimal = function(n) {
			return Math.abs(this.toFixed(n));
		}
		
		//	++ Convert to EM (base size 12px)
		//----------------------------------------------
		Number.prototype.toEm = function() {
			return (this/12).tidyDecimal(3);
		}
		
		//	++ Right and Left cut
		//----------------------------------------------
		String.prototype.left = function(n) {
			return this.substr(0,n);
		};
		String.prototype.right = function(n) {
			return this.substr((this.length-n),this.length);
		};
		
		//	++ Convert date object into friendly string
		//----------------------------------------------
		Date.prototype.toClean=function(){
			if(this!==null){
				var vDay = ((this.getDate())<10)?'0'+(this.getDate()):(this.getDate()),
						oMonths = ['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec'],
						vMonth = oMonths[this.getMonth()],
						vYear = this.getFullYear().toString().right(2);
						
				return vDay+' '+vMonth+' \''+vYear;
			} else {
				return '[Invalid Date]';
			}
		}
		
		//	++ Convert date object into SQL supported
		//----------------------------------------------
		Date.prototype.toSQL=function(){
			var vDay = ((this.getDate())<10)?'0'+(this.getDate()):(this.getDate()),
				nMonth = (this.getMonth()+1),
				vMonth = (nMonth<10)?'0'+nMonth:nMonth,
				vYear = this.getFullYear().toString(),
				vHours = ((this.getHours())<10)?'0'+(this.getHours()):(this.getHours()),
				vMinutes = ((this.getMinutes())<10)?'0'+(this.getMinutes()):(this.getMinutes()),
				vSeconds = ((this.getSeconds())<10)?'0'+(this.getSeconds()):(this.getSeconds());
				
			return vDay+'/'+vMonth+'/'+vYear+' '+vHours+':'+vMinutes+':'+vSeconds;
		}
		
		//	++ Remove all punctuation
		//----------------------------------------------
		String.prototype.clearPunc=function(){
			return this.replace(/[\.,-\/#!$%\^&\*;:{}=\-_`~()]/g,"").replace(/\s{2,}/g," ");
		}
		
		//	++ Highlight words by passed in value
		//----------------------------------------------
		String.prototype.highlight=function(vWords){
			var oWords = vWords.clearPunc().stripTags().split(' '),vNewPhrase=this;
			oWords.each(function(o){
				vNewPhrase=vNewPhrase.replace(new RegExp("("+o+")","ig"),'<span class="highlight">$1</span>');
			});
			return vNewPhrase;
		}
		

		// ++ Run Site
		document.observe("dom:loaded",site.setup.run);